package com.ztom.top100;

/**
 * 零钱兑换
 * <p>
 * https://leetcode-cn.com/problems/coin-change/
 *
 * @author ZhangTao
 */
public class Code80CoinChange {

    public int coinChange2(int[] coins, int amount) {
        if (coins == null || coins.length == 0 || amount < 0) {
            return -1;
        }
        if (amount == 0) {
            return 0;
        }
        int min = process(coins, 0, amount);
        return min == Integer.MAX_VALUE ? -1 : min;
    }

    private int process(int[] coins, int i, int rest) {
        if (i == coins.length) {
            // 如果全部硬币尝试完, 剩余 0 说明是有效尝试
            return rest == 0 ? 0 : Integer.MAX_VALUE;
        }
        int min = Integer.MAX_VALUE;
        // i 位置硬币, 尝试去凑齐 rest
        for (int zhang = 0; zhang * coins[i] <= rest; zhang++) {
            // 去尝试用下一个位置凑
            int next = process(coins, i + 1, rest - zhang * coins[i]);
            // 下一个位置尝试有效
            if (next != Integer.MAX_VALUE) {
                // 求最小张数
                min = Math.min(min, next + zhang);
            }
        }
        return min;
    }

    public int coinChange1(int[] coins, int amount) {
        if (coins == null || coins.length == 0 || amount < 0) {
            return -1;
        }
        if (amount == 0) {
            return 0;
        }
        int n = coins.length;
        int[][] dp = new int[n + 1][amount + 1];
        dp[n][0] = 0;
        // 最后一行
        for (int j = 1; j <= amount; j++) {
            dp[n][j] = Integer.MAX_VALUE;
        }
        for (int i = n - 1; i >= 0; i--) {
            for (int rest = 0; rest <= amount; rest++) {
                int min = Integer.MAX_VALUE;
                // i 位置硬币, 尝试去凑齐 rest
                for (int zhang = 0; zhang * coins[i] <= rest; zhang++) {
                    // 去尝试用下一个位置凑
                    int next = dp[i + 1][rest - zhang * coins[i]];
                    // 下一个位置尝试有效
                    if (next != Integer.MAX_VALUE) {
                        // 求最小张数
                        min = Math.min(min, next + zhang);
                    }
                }
                dp[i][rest] = min;
            }
        }

        return dp[0][amount] == Integer.MAX_VALUE ? -1 : dp[0][amount];
    }

    /**
     * 斜率优化
     *
     * @param coins
     * @param amount
     * @return
     */
    public int coinChange0(int[] coins, int amount) {
        if (coins == null || coins.length == 0 || amount < 0) {
            return -1;
        }
        if (amount == 0) {
            return 0;
        }
        int n = coins.length;
        int[][] dp = new int[n + 1][amount + 1];
        dp[n][0] = 0;
        // 最后一行
        for (int j = 1; j <= amount; j++) {
            dp[n][j] = Integer.MAX_VALUE;
        }
        for (int i = n - 1; i >= 0; i--) {
            for (int rest = 0; rest <= amount; rest++) {
                dp[i][rest] = dp[i + 1][rest];
                if (rest - coins[i] >= 0 && dp[i][rest - coins[i]] != Integer.MAX_VALUE) {
                    dp[i][rest] = Math.min(dp[i][rest], dp[i][rest - coins[i]] + 1);
                }
            }
        }

        return dp[0][amount] == Integer.MAX_VALUE ? -1 : dp[0][amount];
    }


    /**
     * 另一种尝试
     *
     * @param coins
     * @param amount
     * @return
     */
    public int coinChange3(int[] coins, int amount) {
        if (coins == null || coins.length == 0 || amount < 0) {
            return -1;
        }
        if (amount == 0) {
            return 0;
        }
        return process3(coins, amount, new int[amount + 1]);
    }

    private int process3(int[] coins, int rest, int[] dp) {
        if (rest < 0) {
            return -1;
        }
        if (rest == 0) {
            // 无剩余
            return 0;
        }
        if (dp[rest] != 0) {
            return dp[rest];
        }
        int min = Integer.MAX_VALUE;
        for (int coin : coins) {
            int next = process3(coins, rest - coin, dp);
            if (next >= 0 && next < min) {
                min = next + 1;
            }
        }
        dp[rest] = min == Integer.MAX_VALUE ? -1 : min;
        return dp[rest];
    }

    /**
     * 最优解
     *
     * @param coins
     * @param amount
     * @return
     */
    public int coinChange(int[] coins, int amount) {
        if (coins == null || coins.length == 0 || amount < 0) {
            return -1;
        }
        if (amount == 0) {
            return 0;
        }
        int[] dp = new int[amount + 1];

        for (int rest = 1; rest <= amount; rest++) {
            int min = Integer.MAX_VALUE;
            for (int coin : coins) {
                if (rest >= coin) {
                    int next = dp[rest - coin];
                    if (next >= 0 && next < min) {
                        min = next + 1;
                    }
                }
            }
            dp[rest] = min == Integer.MAX_VALUE ? -1 : min;
        }

        return dp[amount];
    }
}
